using System;
using System.BusinessObjects.Transactions;
using Blazing.BusinessObjects.Tagging;
using NHibernate;
using NHibernate.Criterion;

namespace Blazing.BusinessObjects.Queries
{
    public class QrySearchTaxonomyTagsByTaxonomy
    {
        public static ICriteria Query(int taxonomyID)
        {           
            ICriteria qry = UnitOfWork.CurrentSession.CreateCriteria(typeof(TaxonomyTag));           
            qry.Add(Expression.Eq("Taxonomy.ID", taxonomyID));                 
            return qry; 
        }

        public static IQuery Query(int taxonomyID, int parentID, bool attachedOnly)
        {
            string hql;
            string prnt;
            IQuery qry;
            if (parentID > 0)
                prnt = ".ID=" + parentID.ToString();
            else
                prnt = " is null";
            if (attachedOnly)
            {
                //hql = string.Format("select distinct t from Building b join b.BuildingTaxonomyTags t where t.ParentTaxonomyTag{0} and t.Taxonomy.ID={1}", prnt, taxonomyID); 
                hql = string.Format("select distinct t from TaxonomyTag t where t.ParentTaxonomyTag{0} and t.Taxonomy.ID={1}", prnt, taxonomyID); 
            }
            else
            {
                hql = string.Format("select tag from TaxonomyTag tag where ParentTaxonomyTag{0} and t.Taxonomy.ID={1}", prnt, taxonomyID);                
            }
            qry = UnitOfWork.CurrentSession.CreateQuery(hql);
            return qry;
        }
    }
}
